<!-- 人资 -->
<template>
    <ContentWrap>
        <div class="flex justify-between">
            <div class="flex aic jcc fontz fonts16 fontw700">
                人资招聘
            </div>
            <div class="flex aic jcc fontz fonts16 fontw700">
                <ElButton type="primary">新投集团招聘</ElButton>
            </div>
        </div>
        <el-form ref="ruleFormRef" :model="state.ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm"
            status-icon style="margin-top:30px;" label-position="left">
            <div class="flex jcsb">
                <el-form-item label="公司名称" prop="name">
                    <el-input :disabled="!state.settingBool" v-model="state.ruleForm.name" placeholder="请输入公司名称" />
                </el-form-item>
            </div>

            <el-form-item label="公司简介" prop="content">
                <!-- <Editor :disabled="!state.settingBool"/> -->
                <el-input :disabled="!state.settingBool" v-model="state.ruleForm.content" placeholder="请输入公司简介"
                    :autosize="{ minRows: 2 }" type="textarea" rows="10" />
            </el-form-item>
            <el-form-item>
                <div class="flex jcfe w-[100%]">
                    <el-button type="primary" v-if="!state.settingBool" @click="setClick">设置</el-button>
                    <el-button type="primary" v-if="state.settingBool" @click="submitForm(ruleFormRef)">保存</el-button>
                    <el-button v-if="state.settingBool" @click="resetForm">取消</el-button>
                </div>
            </el-form-item>
        </el-form>
    </ContentWrap>
</template>

<script lang="tsx" setup>
import { getTreeLists} from "@/api/column/index"
import { getBaseApi, getBaselistApi, addBaseApi, putBaseApi, addsDeteleBaseApi, addsBaseApi, deteleBaseApi, getDetailApi } from "@/api/setting/index";
// import { Editor } from "@/components/Editor/index"
import { ElButton, ElInput, ElMessage, ElMessageBox } from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { ref, reactive, unref } from "vue"
import { useI18n } from '@/hooks/web/useI18n'
import type { FormInstance, FormRules } from 'element-plus'
import { getHomeConfigurationListApi, addHomeConfigurationListApi, editHomeConfigurationListApi, updateHomeConfigurationListApi } from "@/api/setting/index"
const { t } = useI18n()
const state = reactive({
    baseTitle:"renzi",
    baseType:"",
    ruleForm: {} as any,
    settingBool: false
})
interface RuleForm {
    name: string
    content: string
}
const ruleFormRef = ref<FormInstance>()
const rules = reactive<FormRules<RuleForm>>({
    name: [
        { min: 1, max: 50, message: '请输入1~50字符', trigger: 'blur' },
    ],
    content: []
})
const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log('submit!')
            let params = [
                {
                    contentTitle: "公司名称",
                    contentContents: state.ruleForm.name,
                    baseTitle: "renzi",
                    baseType: "3",
                    contentType: "1"
                },
                {
                    contentTitle: "公司简介",
                    contentContents: state.ruleForm.content,
                    baseTitle: "renzi",
                    baseType: "3",
                    contentType: "2"
                },
            ]
            addsDeteleBaseApi(params).then((res: any) => {
                ElMessage({
                    type: 'success',
                    message: `${res.msg}`,
                })
                resetForm()
                getHomeConfigurationListApi_()
            }).catch((res: any) => {
                ElMessage({
                    type: 'error',
                    message: `${res.msg}`,
                })
            })

        } else {
            console.log('error submit!', fields)
        }
    })
}
const setClick = () => {
    state.settingBool = true
}
const resetForm = () => {
    state.settingBool = false
}
const getHomeConfigurationListApi_ = async () => {
    let params = { baseTitle: "renzi" }
    getBaseApi(params).then((res: any) => {
        console.log(res);
        if (res.data.length != 0) {
            state.ruleForm.name = res.data[0].contentContents
            state.ruleForm.content = res.data[1].contentContents
        }
    })
}
const getTreeListApi = async()=>{
    let params={}
    getTreeLists(params).then(res=>{
        console.log("详情：",res);
        res.data.forEach(element => {
            traverseTree(element)
        });
        console.log(state.baseType);
        getHomeConfigurationListApi_()
    })
}
function traverseTree(node) {
    if(node.data.baseTitle==state.baseTitle){
        state.baseType=node.data.baseType
    }
    if (node.children && node.children.length > 0) {
        for (let child of node.children) {
            traverseTree(child); // 递归遍历子节点  
        }
    }
}
getTreeListApi()

</script>

<style lang="less" scoped></style>